#include "iot/thing.h"
#include <driver/uart.h>
#include "board.h"
#include "audio_codec.h"
#include "application.h"
#include <esp_log.h>
#include <unordered_map>

#define TAG "Action"

#define uart_num UART_NUM_1

namespace iot {

// 这里仅定义 Lamp 的属性和方法，不包含具体的实现
class Action : public Thing {
private:
    
    std::unordered_map<std::string, std::string> action_map = {
        {"standUp", "a"}, {"attention", "a"}, //support many commands related to one action, such as "standUp" and "attention" all map to "a" in the seria
        {"forward", "b"}, {"lieDown", "e"},
        {"dance", "d"}, {"turnLeft", "g"}, {"turnRight", "h"},
        {"twist", "m"}, {"swing", "o"}, {"stretch", "q"},
        {"lookUp", "r"}, {"raiseFeet", "p"}, {"declaration", "u"},
        {"sitDown", "v"}, {"slackOff", "w"}, {"FreeTime", "x"},
        {"retreat", "y"}
    };
    std::unordered_map<std::string, std::string> lighting_map = {
        {"turnOn", "i"}, {"turnOff", "j"}, {"BreathingLight", "k"},
        {"flashlight", "l"}, {"Rainbow", "n"}, {"Flowinglight", "z"},
        {"RandomLight", "s"}, {"Brighten", "A"}, {"Dimming", "B"},
        {"redLamp", "C"}, {"OrangeLamp", "D"}, {"yellowLamp", "E"},
        {"greenLamp", "F"}, {"blueLamp", "G"}, {"wordLamp", "H"}
    };
    // 定义 face_map，根据 case 中的值初始化，初始指令为 "unknow"
    std::unordered_map<std::string, std::string> face_map = {
        {"unknow", "M"}, {"unknow", "O"}, {"unknow", "P"}, {"unknow", "Q"}, {"unknow", "R"},
        {"unknow", "T"}, {"unknow", "U"}, {"unknow", "V"}, {"unknow", "W"}, {"unknow", "X"},
        {"unknow", "Y"}, {"unknow", "c"}, {"unknow", "f"}, {"unknow", "I"}, {"unknow", "J"},
        {"unknow", "K"}, {"unknow", "L"}, {"unknow", "N"}, {"unknow", "S"}, {"unknow", "t"}
    };

public:
    Action() : Thing("Robot", "这是一个机器人，可执行动作和设置灯光") {
        static std::string action_name = "";
        static std::string lighting_mode = "";
        properties_.AddStringProperty("action_name", "动作名称", [this]() -> std::string {            
            return action_name;
        });
        properties_.AddStringProperty("lighting_mode", "灯光模式", [this]() -> std::string {            
            return lighting_mode;
        });
        methods_.AddMethod("setAction", "设置机器人动作", ParameterList({
            Parameter("action_name", "动作名称, attention, forward, getDown,dance,turnLeft,turnRight,\
                Twist,swing,stretch,lookUp,raiseFeet,declaration,coquetry,slackOff,FreeTime,retreat", kValueTypeString, true)
        }), [this](const ParameterList& parameters) {
            action_name = static_cast<std::string>(parameters["action_name"].string());
            // 新增串口发送逻辑
            if (action_map.find(action_name) != action_map.end()) {
                const std::string& cmd = action_map[action_name];
                uart_write_bytes(uart_num, cmd.c_str(), 1);
                ESP_LOGI(TAG, "已发送动作指令[%s]: %s", action_name.c_str(), cmd.c_str());
            } else {
                ESP_LOGE(TAG, "无效动作名称: %s", action_name.c_str());
            }
            
        });
        methods_.AddMethod("setlighting", "设置灯光模式", ParameterList({
            Parameter("lighting_mode", "灯光模式, turnOn,turnOff,BreathingLight,flashlight,Rainbow,\
                Flowinglight,RandomLight,Brighten,Dimming,redLamp,OrangeLamp,yellowLamp,greenLamp,blueLamp,wordLamp", kValueTypeString, true)
        }), [this](const ParameterList& parameters) {
            lighting_mode = static_cast<std::string>(parameters["lighting_mode"].string());
            // 新增串口发送逻辑
            if (lighting_map.find(lighting_mode) != lighting_map.end()) {
                const std::string& cmd = lighting_map[lighting_mode];
                uart_write_bytes(uart_num, cmd.c_str(), 1);
                ESP_LOGI(TAG, "已发送灯光模式:[%s] %s", lighting_mode.c_str(), cmd.c_str());
            } else {
                ESP_LOGE(TAG, "无效灯光模式: %s", lighting_mode.c_str());
            }
            
        });
        
    }
};

} // namespace iot

DECLARE_THING(Action);
